package chapter18.tankwar;

/**
 * @Auther: Tx
 * @Date: 2022/7/14 09:58
 */
public class Bullet implements Runnable {
    int x;//子弹的横坐标
    int y;//子弹的纵坐标
    int direction = 0;//子弹的方向
    int speed = 2;
    boolean isLive = true;

    public Bullet(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    @Override
    public void run() {

        while (true) {
//            休眠50毫秒，以防止速度太快，子弹轨迹不连贯
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            switch (direction) {
                case 0: //向上开火
                    y -= speed;
                    break;
                case 1://向右
                    x += speed;
                    break;
                case 2://向下
                    y += speed;
                    break;
                case 3:
                    x -= speed;
                    break;
            }
            System.out.println("x=" + x + " y=" + y);

//超出边界，子弹线程结束
            if (!((x > 0 && x < 1000) && (y > 0 && y < 750))) {
                System.out.println("子弹线程结束");
                isLive = false;
                break;
            }
        }
    }
}

